/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.reciever.encryptor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author julian.pena
 */
public class AsymmetricEncriptor implements Encryptor{
    
    private long privateKey;
    private long publicKey;
    private String location;

    //<editor-fold defaultstate="collapsed" desc="constructors">
    public AsymmetricEncriptor(long privateKey, long publicKey, String location) {
        this.privateKey = privateKey;
        this.publicKey = publicKey;
        this.location = location;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="methods">
    @Override
    public void encode() throws Exception {
        File file = new File(location);
        if (!file.exists()) {
            throw new FileNotFoundException("Archivo no encontrado");
        }
        if (file.isDirectory()) {
            String[] files = file.list();
            for (String string : files) {
                File temp = new File(string);
                if (temp.isFile()) {
                    File newFile = new File(location + ".cryp");
                    if (!temp.renameTo(newFile)) {
                        throw new Exception("Error renombrando archivo");
                    }
                }
            }
        } else {
            File newFile = new File(location + ".cryp");
            //Rename
            if (!file.renameTo(newFile)) {
                throw new Exception("Error renombrando archivo");
            }
        }
    }
    
    @Override
    public void action() throws Exception {
        encode();
    }
    
    @Override
    public InputStream getInputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setInputStream(InputStream inputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public OutputStream getOutputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setOutputStream(OutputStream outputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void decode() throws Exception {
        File file = new File(location);
        if (!file.exists()) {
            throw new FileNotFoundException("Archivo no encontrado");
        }
        if (file.isDirectory()) {
            String[] files = file.list();
            for (String string : files) {
                File temp = new File(location+ "/" + string);
                if (temp.isFile()) {
                    String path = temp.getAbsolutePath();
                    if (!path.substring(path.length() - 5, path.length()).equals(".cryp")) {
                        throw new Exception("archivo no encriptado");
                    }
                    File newFile = new File(path.substring(0, path.length()-5));
                    if (!temp.renameTo(newFile)) {
                        throw new Exception("Error renombrando archivo");
                    }
                }
            }
        } else {
            String path = file.getAbsolutePath();
            if (!path.substring(path.length() - 5, path.length()).equals(".cryp")) {
                throw new Exception("archivo no encriptado");
            }
            File newFile = new File(path.substring(0, path.length()-5));
            if (!file.renameTo(newFile)) {
                throw new Exception("Error renombrando archivo");
            }
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="getters and setters">
    public long getPrivateKey() {
        return privateKey;
    }
    
    public long getPublicKey() {
        return publicKey;
    }
    
    public String getLocation() {
        return location;
    }
    //</editor-fold>
}
